package com.ydl.shopping.controller.shopping;

import com.ydl.shopping.annotation.Logger;
import com.ydl.shopping.core.controller.BaseController;
import com.ydl.shopping.core.dto.OrderDTO;
import com.ydl.shopping.core.vo.ResultVO;
import com.ydl.shopping.domain.TbOrder;
import com.ydl.shopping.domain.TbShoppingCart;
import com.ydl.shopping.service.ITbOrderService;
import com.ydl.shopping.service.ITbShoppingCartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单相关接口
 *
 * @author 林武泰
 */
@RestController
@CrossOrigin
@RequestMapping("/shopping/order")
@Tag(name = "订单Controller")
public class TbOrderController extends BaseController {
    @Resource
    private ITbOrderService service;

    @PostMapping("/create")
    @Operation(summary = "创建订单")
    public ResultVO create(@RequestBody List<OrderDTO> entity) {
        return success(service.createOrder(entity));
    }

    @DeleteMapping("/cancel/{number}")
    @Operation(summary = "取消订单")
    public ResultVO cancel(@PathVariable("number") String orderNUmber) {
        return toResult(service.cancelOrder(orderNUmber));
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('order:query')")
    @Operation(summary = "根据id查询订单")
    @Logger(model = "订单管理")
    public ResultVO getById(@PathVariable("id") Long id) {
        return success(service.getById(id));
    }

    @PostMapping("/list")
    @PreAuthorize("hasAnyAuthority('order:query')")
    @Operation(summary = "查询订单列表")
    @Logger(model = "订单管理")
    public ResultVO list(@RequestBody TbOrder entity) {
        // 获取页码
        Long pageNumber = entity.getPageNumber();
        // 获取页数
        Long pageSize = entity.getPageSize();
        // 开启分页
        startPage(pageNumber, pageSize);

        return getPageResult(service.list(service.queryWrapper(entity)));
    }

    @PostMapping
    @PreAuthorize("hasAnyAuthority('order:save')")
    @Operation(summary = "新增订单")
    @Logger(model = "订单管理")
    public ResultVO save(@Validated @RequestBody TbOrder entity) {
        entity.setCreateBy(username());
        return toResult(service.save(entity));
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('order:update')")
    @Operation(summary = "修改订单")
    @Logger(model = "订单管理")
    public ResultVO update(@Validated @RequestBody TbOrder entity) {
        entity.setUpdateBy(username());
        return toResult(service.updateById(entity));
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyAuthority('order:delete')")
    @Operation(summary = "删除订单")
    @Logger(model = "订单管理")
    public ResultVO deleteById(@PathVariable("id") Long id) {
        return toResult(service.removeById(id));
    }

    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('order:delete')")
    @Operation(summary = "批量删除订单")
    @Logger(model = "订单管理")
    public ResultVO deleteByIds(@RequestParam List<Long> ids) {
        return toResult(service.removeByIds(ids));
    }
}
